package com.mx.zb.mng.ctrl;

import java.util.List;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.mx.zb.mng.bo.Agent;
import com.mx.zb.mng.bo.Module;

import com.mx.zb.mng.bo.Operator;
import com.mx.zb.mng.bo.Role;

import com.mx.zb.mng.dao.AgentMapper;
import com.mx.zb.mng.dao.ModuleMapper;

import com.mx.zb.mng.dao.OperatorMapper;
import com.mx.zb.mng.dao.RoleMapper;
import com.mx.zb.mng.dao.RoleModuleRelaMapper;
import com.mx.zb.mng.utils.Constant;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.ex.PageAnnotation;



/**
 * 
 * @ClassName: SystemCtrl
 * @Description: 系统管理
 * @author 张博
 * @date 2017年9月13日 下午10:08:07 
 *
 */
@Controller
@PageAnnotation
@RequestMapping("/p/sys")
public class SystemCtrl {
	
	@Autowired
	RoleMapper roleMapper;
	
	@Autowired
	OperatorMapper operatorMapper;
	
	@Autowired
	ModuleMapper moduleMapper;
	
	@Autowired
	AgentMapper agentMapper;
	
	@Autowired 
	RoleModuleRelaMapper roleModuleRelaMapper;
 	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
/**
 * 系统管理模块   进入角色管理         
 *  coding by 张衡
 * @param model
 * @return
 */
	@RequestMapping(value= {"/role"}, method= {RequestMethod.GET})
	public String toRoleManage(Model model) {
		String agentId = ContextThread.get().getAgentId();
		Agent agent = agentMapper.selectByPrimaryKey(agentId);
		
		int[] roleSets = null;
		if(Constant.headquarters.equals(agent.getType())) {
			roleSets=new int[]{1,3,4};
			
		}else if(Constant.distributor.equals(agent.getType())) {
			roleSets=new int[]{1,2,3};
		}else {
			roleSets=new int[]{1};
		}
		
		List<Module> mList = moduleMapper.selectAllForRela(roleSets);
		model.addAttribute("mList", mList);
		return "role_manage";
	}
	@RequestMapping(value= {"/operator"}, method= {RequestMethod.GET})	
	public String toOperator(Model model) {
		String agentId = ContextThread.get().getAgentId();
		List<Role> roles=roleMapper.findAllByAgent(agentId);
		model.addAttribute("roles",roles);
		/*Agent agent = agentMapper.selectByPrimaryKey(agentId);
		int[] roleSets = null;
		if(Const.agentType1.equals(agent.getType())) {
			roleSets=new int[]{1,3,4};
			
		}else if(Const.agentType2.equals(agent.getType())) {
			roleSets=new int[]{1,2,3};
		}else {
			roleSets=new int[]{1};
		}
		List<String> roleIds=new ArrayList<String>();
		List<Module> mList = moduleMapper.selectAllForRela(roleSets);
		model.addAttribute("mList", mList);*/
		
		/*for(Role role:roles) {
			List<String> list=roleModuleRelaMapper.selectIds(role.getId());
			List<Module> modules=new ArrayList<Module>();
			for(String mid:list) {
				modules.add(moduleMapper.selectByPrimaryKey(mid));
			}
			role.setModules(modules);
		}*/
		return "operator";
	}

	@RequestMapping(value= {"/me"}, method= {RequestMethod.GET})
	public String toMe(Model model) {
		String operatorId = ContextThread.get().getOperatorId();
		Operator o = operatorMapper.selectByPrimaryKey(operatorId);
		model.addAttribute("entity",o);
		return "me";
	}
	

	@RequestMapping(value= {"/qiniu"}, method= {RequestMethod.GET})
	public String toQinniu(Model model) {
		
		return "qiniu";
	}
	
	
	
	

}
